#!/bin/bash
Deploy_Path=`cd $(dirname $0);pwd -P`
source $Deploy_Path/ceph_cluster.env


remove_deploy_logs(){
    rm -f $Deploy_Path/logs/*
}

stop_ceph_services(){

    systemctl stop ceph-*.service
    systemctl stop ceph-mon@`hostname` ceph-mds@`hostname` ceph-mgr@`hostname`
    systemctl disable ceph-mon@`hostname` ceph-mds@`hostname` ceph-mgr@`hostname`
    systemctl disable ceph* 
    systemctl stop pmcd pmlogger pmie
    systemctl disable pmcd pmlogger pmie
}

kill_all_ceph_proc(){

    CEPH_PROC=$(ps -aux|grep ceph|grep -v grep|grep -v destroy |awk '{print $2}')
    for i in $CEPH_PROC;do
        kill -9 $i
    done
    sleep 12
}

umount_ceph_volumes(){
    #umount
    mounts=`mount |grep ceph|awk '{print $3}'`
    for i in $mounts;do
        umount $i
    done
    
    #systemd disabled mount 
    osds=`ls /var/lib/ceph/osd-env/|grep -v cache|awk -F '.' '{print $NF}'`
    for i in $osds;do
        systemctl stop var-lib-ceph-osd-ceph\\x2d$i.mount
        systemctl disable var-lib-ceph-osd-ceph\\x2d$i.mount
    done

    #remove systemd mount unit files
    mount_units=`ls /usr/lib/systemd/system/|grep mount|grep var-lib-ceph-osd`
    for i in $mount_units;do
        rm -f /usr/lib/systemd/system/$i
    done
}

remove_ceph_files(){
    rm -rf /etc/ceph/* 
    rm -rf /var/lib/ceph/bootstrap-osd/*
    rm -rf /var/lib/ceph/bootstrap-mon/*
    rm -rf /var/lib/ceph/bootstrap-mds/*
    rm -rf /var/lib/ceph/bootstrap-mgr/*
    rm -rf /var/lib/ceph/mon/* 
    rm -rf /var/lib/ceph/mgr/* 
    rm -rf /var/lib/ceph/mds/* 
    rm -rf /var/lib/ceph/tmp/* 
    rm -rf /var/log/ceph/* 
    rm -rf /var/lib/ceph/osd-env/*
    rm -rf /var/lib/ceph/osd/*
    rm -f  $Deploy_Path/ceph-openstack.tar.gz
    rm -rf $Deploy_Path/openstack/*
}

clean_bcache_mapping(){
    if [ -d /sys/fs/bcache ];then
        BCache_UUID=`ls /sys/fs/bcache/ |sed -n '/.*-.*-.*-.*-.*/p'`
        if [ ! -z "$BCache_UUID" ];then
            for i in $BCache_UUID;do
                echo 1 > /sys/fs/bcache/$i/stop
                echo 1 > /sys/fs/bcache/$i/unregister
            done
        fi
        sleep 5
    fi
}

clean_bcache_mapping_force(){
    root_disk=`lsblk -l|sed -n '/\/$/p'|awk '{print $1}'|sed -n 's/[0-9]\+//p'`
    Block_BCaches=`ls /sys/block/ |grep bcache|grep -v -w "$root_disk"`
    for i in $Block_BCaches;do
        if [ -f /sys/block/$i/bcache/stop ];then
            echo echo 1 > /sys/block/$i/bcache/stop
        fi
    done
    sleep 5

    if [ -d /sys/fs/bcache ];then
        for i in $BCache_UUID;do
            BCache_Dev=`ls -l /sys/fs/bcache/$BCache_UUID/|grep block|grep devices|egrep ^l|awk -F'/' '{print $(NF-1)}'`
            if [ ! -z "$BCache_Dev" ];then
                dd if=/dev/zero of=/dev/$BCache_Dev bs=4M count=1 oflag=dsync
            else
                echo 'BCache_Dev is null'
            fi
        done
    fi
}

wipe_all_parts_data(){
  #Disks=`echo $Parts|sed -n 's/[0-9]\+$//p'|sort -u`
    root_disk=`lsblk -l|sed -n '/\/$/p'|awk '{print $1}'|sed -n 's/[0-9]\+//p'`
    Disks=`lsblk -l|grep disk|awk '{print $1}'|grep -v -w "$root_disk"`
    Parts=`lsblk -l|grep part|sed -n '/part *$/p'|awk '{print $1}'|egrep -v "$root_disk[0-9]+]"`
    for d in $Disks;do

        echo $Parts|grep "$d"
        if [ "$?"x == "0"x ];then
            Disk_Parts=`echo $Parts|grep $d`
            for p in $Disk_Parts;do
                if [ -f /sys/block/$d/$p/bcache/stop ];then
                    echo 1 > /sys/block/$d/$p/bcache/stop
                fi
                if [ -f /sys/block/$d/$p/bcache/unregister ];then
	            echo 1 > /sys/block/$d/$p/bcache/unregister
                fi
	        lsblk -l|grep part|sed -n '/part *$/p'|awk '{print $1}' |grep -w $p
	        if [ "$?" == "0" ];then
	            dd if=/dev/zero of=/dev/$p bs=4M count=1 oflag=dsync
	        fi
            done    
        fi
        if [ -f /sys/block/$d/bcache/stop ];then
            echo 1 > /sys/block/$d/bcache/stop
        fi
        if [ -f /sys/block/$d/bcache/unregister ];then
            echo 1 > /sys/block/$d/bcache/unregister
        fi
        dd if=/dev/zero of=/dev/$d bs=8M count=1 oflag=dsync
        partprobe /dev/$d
    done
}

set_disk_owner(){
    chown -R root:root /dev/
    sync & wait
}

restart_udev(){
    systemctl restart systemd-udevd

}

remove_ceph_packages(){
    source /etc/os-release
    case $ID in
        ubuntu)
            PKGS=`apt-cache pkgnames|grep ceph`
            apt-get remove -qy $PKGS
            apt-get autoremove -qy
            ;;
        debain)
            PKGS=`apt-cache pkgnames|grep ceph`
            apt-get remove -qy $PKGS
            apt-get autoremove -qy
            ;;
        centos)
            PKGS=`yum list -y --installed|grep ceph`
            yum remove -y $PKGS
            ;;
        *)
            echo "The Operating System is not supported !"
            ;;
    esac

}

destroy_ceph_all(){
    remove_deploy_logs
    stop_ceph_services
    kill_all_ceph_proc
    umount_ceph_volumes
    remove_ceph_files
    systemctl daemon-reload
    clean_bcache_mapping
    clean_bcache_mapping_force
    wipe_all_parts_data
    set_disk_owner
    restart_udev
}
if [ "$1"x == "--deepx" ];then
    destroy_ceph_all
    remove_ceph_packages
else
    destroy_ceph_all
fi

#reboot
